package com.aimuceliang.aimuceliang.ui.activity;

import android.widget.EditText;

import com.aimuceliang.aimuceliang.MyApp;
import com.aimuceliang.aimuceliang.AppManager;
import com.aimuceliang.aimuceliang.bean.HttpResult;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.net.HttpSubscriber;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.utils.SharePreferenceUtil;
import com.aimuceliang.aimuceliang.utils.StringUtil;
import com.aimuceliang.aimuceliang.utils.T;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ziabo on 2017/7/27.
 * 修改密码界面
 */
public class ModifyPwdActivity extends BaseActivity {
    @BindView(R.id.et_old_pwd)
    EditText vEtOldPwd;
    @BindView(R.id.et_new_pwd)
    EditText vEtNewPwd;
    @BindView(R.id.et_new_pwd_ensure)
    EditText vEtNewPwdEnsure;

    @Override
    protected int initContentView() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    protected void initUIAndListener() {
        setTitle("修改密码");
    }

    @Override
    protected void initData() {

    }


    @OnClick(R.id.tv_submit)
    public void onClick() {
        String oldPwd = vEtOldPwd.getText().toString().trim();
        String newPwd = vEtNewPwd.getText().toString().trim();
        String newPwdEnsure = vEtNewPwdEnsure.getText().toString().trim();
        if (StringUtil.checkStr(oldPwd)) {
            if (!SharePreferenceUtil.getPassword(this).equals(oldPwd)) {
                T.showShort("请输入正确的旧密码");
            } else {
                if (!StringUtil.isPassword(newPwd)) {
                    T.showShort("请输入正确6-13位新密码");
                    return;
                }
                if (oldPwd.equals(newPwd)) {
                    T.showShort("新旧密码不能相同");
                    return;
                }
                if (!newPwd.equals(newPwdEnsure)) {
                    T.showShort("两次输入密码不同");
                    return;
                }
                addSubscription(MyApp.getApiService().modifyPwd(new HttpSubscriber<HttpResult>() {
                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onNext(HttpResult httpResult) {
                        if (httpResult != null) {
                            if (StringUtil.checkStr(httpResult.code)) {
                                if (httpResult.code.equals("0")) {
                                    // TODO: 2017/8/2 此处加一个dialog做家延迟
                                    SharePreferenceUtil.savePassword(ModifyPwdActivity.this,newPwd);
                                    startActivity(LoginActivity.class);
                                    AppManager.getAppManager().finishAllActivity();
                                } else {
                                    T.showShort(httpResult.message);
                                }
                            }
                        }
                    }
                }, SharePreferenceUtil.getUsername(this), oldPwd, newPwd));
            }
        } else {
            T.showShort("旧密码不能为空");
        }
    }
}
